package cn.kgc.controller;

import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import cn.kgc.bean.pojo.CCase;
import cn.kgc.common.Constants;
import cn.kgc.common.Result;
import cn.kgc.mapper.CaseMapper;
import cn.kgc.service.CaseService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.net.URLEncoder;
import java.util.List;


@RestController
@RequestMapping("/case")
public class CaseController {
    @Resource
    private CaseService caseService;
    @Resource
    private CaseMapper caseMapper;

    @GetMapping("/getAllInfo")
    public Result getAllInfo(@RequestParam int courtId,
                             @RequestParam(required = false) int pageNum,
                             @RequestParam(required = false) int pageSize,
                             @RequestParam(defaultValue = " ") String caseName,
                             @RequestParam(defaultValue = " ") String num,
                             @RequestParam(defaultValue = " ") String undertaker){
        QueryWrapper<CCase> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("court",courtId);
        if (!" ".equals(caseName)){
            queryWrapper.like("caseName",caseName);
        }
        if (!" ".equals(num)){
            queryWrapper.like("num",num);
        }
        if (!" ".equals(undertaker)){
            queryWrapper.like("undertaker",undertaker);
        }
        queryWrapper.orderByDesc("id");
        Page<CCase> cCasePage = caseMapper.selectPage(new Page<>(pageNum, pageSize), queryWrapper);
        if(cCasePage != null){
            return Result.success(cCasePage);
        }else {
            return Result.error();
        }
    }
    @GetMapping("/getCaseInfo")
    public Result getCaseInfo(@RequestParam(defaultValue = "0") long status,
                              @RequestParam String courtId,
                              @RequestParam(defaultValue = " ") String caseName,
                              @RequestParam(required = false) int pageNum,
                              @RequestParam(required = false) int pageSize){
        QueryWrapper<CCase> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status",status);
        queryWrapper.eq("court",courtId);
        if (!" ".equals(caseName)){
            queryWrapper.like("caseName",caseName);
        }
        Page page = caseService.page(new Page<>(pageNum,pageSize),queryWrapper);
        if (page != null){
            return Result.success(Constants.CODE_200,"",page);
        }else {
            return Result.error();
        }
    }

    @GetMapping("/getCaseByid")
    public Result getCaseByid(@RequestParam String id){
        CCase byid = caseService.getCaseByid(Integer.valueOf(id));
        return Result.success(Constants.CODE_200,"",byid);
    }

    @GetMapping("/updateStatus")
    public Result updateStatus(@RequestParam String id){
        CCase cCase = new CCase();
        cCase.setId(Long.parseLong(id));
        boolean flag = caseService.updateById(cCase);
        if (flag == true){
            return Result.success();
        }else {
            return Result.error();
        }
    }

    @PostMapping("/updateStatus1")
    public Result updateStatus1(@RequestBody String id) {
        String msg = "";
        int count = caseService.updateStatus1(Integer.valueOf(id));
        if (count > 0){
            return Result.success();
        }else {
            return Result.error();
        }
    }

    @PostMapping("/updateStatus2")
    public Result updateStatus2(@RequestBody String id) {
        String msg = "";
        int count = caseService.updateStatus2(Integer.valueOf(id));
        if (count > 0){
            return Result.success();
        }else {
            return Result.error();
        }
    }

    @PostMapping("/addCaseInfo")
    public Result addCaseInfo(@RequestBody CCase cCase){
        int count = caseMapper.addCaseInfo(cCase);
        if (count > 0) {
            return Result.success();
        }else {
            return Result.error();
        }
    }

    //导出
    @GetMapping("/export")
    public void export( HttpServletResponse response) throws Exception{
        List<CCase> list = caseMapper.getAllByCourt();

        ExcelWriter writer = ExcelUtil.getWriter(true);
//        writer.addHeaderAlias("name","用户名");
//        writer.addHeaderAlias("workingLife","工作年限");
//        writer.addHeaderAlias("workExperien","工作经历");
//        writer.addHeaderAlias("personalProfil","个人简介");
//        writer.addHeaderAlias("deName","所属部门");
//        writer.addHeaderAlias("graduateScho","毕业院校");
//        writer.addHeaderAlias("password","密码");

        //一次性写出list到excle，使用默认格式，强制输入标题
        writer.write(list,true);

        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
        String filename = URLEncoder.encode("案例信息","UTF-8");
        response.setHeader("Content-Disposition","attachment;filename="+filename+".xlsx");

        ServletOutputStream out = response.getOutputStream();
        writer.flush(out,true);
        out.close();
        writer.close();
    }
    //导入
    @PostMapping("/import")
    public void imp(MultipartFile file) throws Exception{
        InputStream inputStream = file.getInputStream();
        ExcelReader reader = ExcelUtil.getReader(inputStream);
        List<CCase> list = reader.read(0,1,CCase.class);
        for(CCase e:list){
            caseMapper.addCaseInfo(e);
        }
        System.out.println(list);
    }

}
